﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AMapPlugin.Dtos;

namespace AMapPlugin.Handler
{
    public class AuthKeyHandler : DelegatingHandler
    {
        private readonly AmapOptions option;

        public AuthKeyHandler(AmapOptions option)
        {
            this.option = option;
        }


        protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            if (request.RequestUri != null)
            {
                var uriBuilder = new UriBuilder(request.RequestUri);
                var query = System.Web.HttpUtility.ParseQueryString(uriBuilder.Query);

                // 添加参数 a 的值
                query["key"] = option.Key;

                uriBuilder.Query = query.ToString();
                request.RequestUri = uriBuilder.Uri;
            }

            return await base.SendAsync(request, cancellationToken);
        }
    }
}
